using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using BuildBox.Core.Extensions;

namespace BuildBox.Core
{
    public interface IConfiguration
    {
        string VirtualDirectory { get; }
        string WebDirectory { get;  }
        int Port { get; }
    }

    public class BuildBoxConfiguration : IConfiguration
    {
        public BuildBoxConfiguration()
        {
            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            VirtualDirectory = appSettings["buildbox.virtualDir"] ?? string.Empty;
            if (VirtualDirectory.EndsWith("/"))
            {
                VirtualDirectory = VirtualDirectory.Substring(0, VirtualDirectory.Length - 1);
            }

            if (!VirtualDirectory.StartsWith("/"))
            {
                VirtualDirectory = "/" + VirtualDirectory;
            }

            WebDirectory = appSettings["buildbox.webDir"] ?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"WebUI");

            int port;
            if (!int.TryParse(appSettings["buildbox.port"], out port))
            {
                port = 8080;
            }

            Port = port;
        }

        public string VirtualDirectory
        {
            get;
            private set;
        }

        public string WebDirectory
        {
            get;
            private set;
        }

        public int Port
        {
            get;
            private set;
        }
    }
}